package BianMa;

import dao.UserMapper;
import entity.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.Test;
import utils.MyBatisUtil;

import java.util.ArrayList;
import java.util.List;

public class BianMaTest {
    private Logger logger=Logger.getLogger(BianMaTest.class);

    @Test
    public void testupdateUserById(){
        SqlSession sqlSession=null;
        int count=0;
        Integer id=18;
        List<User> userList1=new ArrayList<>();
        List<User> userList2=new ArrayList<>();
        try {
            sqlSession= MyBatisUtil.createSqlSession();
            userList1=sqlSession.getMapper(UserMapper.class).getUserListByRoleId(id);
            userList2=sqlSession.getMapper(UserMapper.class).getUserListByRoleId(id);

            if (userList1==null || userList2==null){
                for (User user:userList1){
                    int result=sqlSession.getMapper(UserMapper.class).BianMadeleteRoleById(user.getId());
                }
                for (User user:userList2){
                    int result=sqlSession.getMapper(UserMapper.class).BianMadeleteRoleById(user.getId());
                }
            }
            if (userList1==null && userList2==null){
                count=sqlSession.getMapper(UserMapper.class).BianMadeleteUserById(id);

            }
            sqlSession.commit();
        }catch(Exception e){
            e.printStackTrace();
            sqlSession.rollback();
            count=0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        logger.debug("testAdd count:"+count);
    }
}
